/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb.persistence;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;

/**
 *
 * @author Julien
 */
@Entity
@Table(name="ORDERS")
@NamedQueries({
        @NamedQuery(name="Order.getOrdersByUser", query="SELECT o FROM Order o WHERE o.user = :user ORDER BY o.id DESC"),
        @NamedQuery(name="Order.getAllOrders", query="SELECT o FROM Order o ORDER BY o.id DESC"),
        @NamedQuery(name="Order.getWaitingOrders", query="SELECT o FROM Order o WHERE o.user = :user AND o.state <> :state ORDER BY o.id DESC"),
        @NamedQuery(name="Order.getOrdersByUserAndState", query="SELECT o FROM Order o WHERE o.user = :user AND o.state = :state ORDER BY o.id DESC"),
        @NamedQuery(name="Order.getOrdersByUserAndTwoStates", query="SELECT o FROM Order o WHERE o.user = :user AND (o.state = :stateOne OR o.state = :stateTwo) ORDER BY o.id DESC"),
        @NamedQuery(name="Order.getOrdersByState", query="SELECT o FROM Order o WHERE o.state = :state ORDER BY o.id DESC")
})
public class Order implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="Id_Order")
    private Long id;

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name="Order_Date")
    private Date orderDate = new Date();
    
    @JoinColumn(name="Id_User")
    @ManyToOne
    private User user;
    
    @Column(name="Types")
    @Enumerated(EnumType.STRING)
    private OrderState state = OrderState.Validée;
    
    @OneToMany(mappedBy = "orders", fetch= FetchType.EAGER)
    private List<Invoice> invoices;
    
    
/* -----------------------------------------------------------------------------
 * ---------------------------GETTERS AND SETTERS-------------------------------
 * ---------------------------------------------------------------------------*/
    public Long getId() {
        return id;
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public OrderState getState() {
        return state;
    }

    public void setState(OrderState state) {
        this.state = state;
    }

    public List<Invoice> getInvoices() {
        return invoices;
    }

    public void setInvoices(List<Invoice> invoices) {
        this.invoices = invoices;
    }

    
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Order)) {
            return false;
        }
        Order other = (Order) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.ejb.persistence.Order[ id=" + id + " ]";
    }
    
}
